package com.tk.exam.web.admin;

import com.alibaba.fastjson.JSONObject;

import com.tk.exam.http.Response;
import com.tk.exam.service.TkQuestionService;
import com.tk.exam.utils.JsonUtils;
import com.tk.exam.utils.PageInfo;
import com.tk.exam.vo.question.*;
import com.tk.exam.vo.question.req.*;
import com.tk.exam.vo.question.resp.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("tk/exam/admin/questions")
@Slf4j
public class TkQuestionsController {

    @Autowired
    TkQuestionService tkQuestionService;

    /**
     * 分页查询以WithPage结尾
     *
     * @param :quesType:1单选题,2:判断题,3:填空题,4:简答题,5:4C题 全部传""
     *          quesCategory:缺陷类型id  全部传""
     * @return
     */
    @RequestMapping(value = "/searchListByPage", method = RequestMethod.POST)
    public Response searchQuestionsListWithPage(@RequestBody JSONObject param) {
        String quesType = param.getString("quesType");
        String quesCategory = param.getString("quesCategory");
        //查询分页数据
        com.github.pagehelper.PageInfo<TkQuestionVo> pageInfoHelper = tkQuestionService.searchList(quesCategory,quesType);
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }


    /**
     * 新增试题
     *
     * @param :quesType:1单选题,2:判断题,3:填空题,4:简答题,5:4C题 全部传""
     *          quesCategory:缺陷类型id  全部传""
     * @return
     */
    @RequestMapping(value = "/add4cQues", method = RequestMethod.POST)
    public Response addTk4cQuestion(@RequestBody JSONObject param) {

        TkQuestion4CRequestVo vo = JsonUtils.string2Object(param.toJSONString(), TkQuestion4CRequestVo.class);
        if(!tkQuestionService.validate4c(vo)){
            return Response.fail("入参错误");
        }
        //入库
        tkQuestionService.save(vo);
        return Response.succeed();
    }
    @RequestMapping(value = "/addDxQues", method = RequestMethod.POST)
    public Response addTkDxQuestion(@RequestBody JSONObject param) {
        TkQuestionDXRequestVo vo = JsonUtils.string2Object(param.toJSONString(), TkQuestionDXRequestVo.class);
        if(!tkQuestionService.validateDx(vo)){
            return Response.fail("入参错误");
        }
        //入库
        tkQuestionService.save(vo);
        return Response.succeed();
    }
    @RequestMapping(value = "/addJsTkQues", method = RequestMethod.POST)
    public Response addTkJsQuestion(@RequestBody JSONObject param) {
        TkQuestionJSRequestVo vo = JsonUtils.string2Object(param.toJSONString(), TkQuestionJSRequestVo.class);
        if(!tkQuestionService.validateJs(vo)){
            return Response.fail("入参错误");
        }
        //入库
        tkQuestionService.save(vo);
        return Response.succeed();
    }
    @RequestMapping(value = "/addPdQues", method = RequestMethod.POST)
    public Response addTkPdQuestion(@RequestBody JSONObject param) {
        TkQuestionPDRequestVo vo = JsonUtils.string2Object(param.toJSONString(), TkQuestionPDRequestVo.class);
        if(!tkQuestionService.validatePd(vo)){
            return Response.fail("入参错误");
        }
        //入库
        tkQuestionService.save(vo);
        return Response.succeed();
    }
    @RequestMapping(value = "/editTkQuestion",method = RequestMethod.POST)
    public Response editTkQuestion(@RequestBody JSONObject param){
        String id = param.getString("id");
        String quesType = param.getString("quesType");
        if(StringUtils.isBlank(id)){
            return Response.fail("id不能为空");
        }
        if(!tkQuestionService.validateType(quesType)){
            return Response.fail("试题类型传参错误");
        }
        TkQuestionRequestVo vo =null;
        if("5".equals(quesType)){
             vo = JsonUtils.string2Object(param.toJSONString(), TkQuestion4CRequestVo.class);
            if(!tkQuestionService.validate4c((TkQuestion4CRequestVo) vo)){
                return Response.fail("4c传参错误");
            }
        }
        if("4".equals(quesType)){
             vo = JsonUtils.string2Object(param.toJSONString(), TkQuestionJSRequestVo.class);
            if(!tkQuestionService.validateJs((TkQuestionJSRequestVo) vo)){
                return Response.fail("js传参错误");
            }
        }
        if("3".equals(quesType) ){
             vo = JsonUtils.string2Object(param.toJSONString(), TkQuestionJSRequestVo.class);
            if(!tkQuestionService.validateJs((TkQuestionJSRequestVo) vo)){
                return Response.fail("tk传参错误");
            }
        }
        if("2".equals(quesType)){
             vo = JsonUtils.string2Object(param.toJSONString(), TkQuestionPDRequestVo.class);
            if(!tkQuestionService.validatePd((TkQuestionPDRequestVo) vo)){
                return Response.fail("pd传参错误");
            }
        }
        if("1".equals(quesType)){
             vo = JsonUtils.string2Object(param.toJSONString(), TkQuestionDXRequestVo.class);
            if(!tkQuestionService.validateDx((TkQuestionDXRequestVo) vo)){
                return Response.fail("dx传参错误");
            }
        }
        tkQuestionService.save(vo);

        return Response.succeed();
    }


    @RequestMapping(value = "/findQuesById",method = RequestMethod.GET)
    public Response findById(@RequestParam String id){
        if(StringUtils.isBlank(id)){
            return Response.fail("入参错误！");
        }
        //组装数据
        TkQuestionTotalVo vo = tkQuestionService.findById(id);
        return Response.succeed(vo);
    }

    @RequestMapping(value = "/checkAnswer",method = RequestMethod.GET)
    public Response checkAnswer(@RequestParam String id){
        if(StringUtils.isBlank(id)){
            return Response.fail("入参错误！");
        }
        //组装数据
        TkAnswerVo vo = tkQuestionService.findAnswerById(id);
        return Response.succeed(vo);
    }

    @RequestMapping(value = "/deleteQues",method = RequestMethod.POST)
    public Response deleteQues(String[] quesIds){
        if (quesIds==null||quesIds.length==0) {
            return Response.fail("试题id不能为空");
        }
        List<String> quesList = Arrays.asList(quesIds);
        tkQuestionService.deleteQues(quesList);
        //组装数据
        return Response.succeed();
    }

}
